package cn.com.ntr.jhclient.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.ntr.jhclient.model.SearchRecord;

public class DBSearchRecord implements DBInterface<SearchRecord> {
	private DatabaseHelper helper;

	public DBSearchRecord(Context context) {
		helper = new DatabaseHelper(context);
	}

	@Override
	public long insert(SearchRecord t) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(SearchRecord.R_KEY, t.getKeyWord());
		values.put(SearchRecord.R_COUNT, t.getRecordCount());
		values.put(SearchRecord.R_ANAME, t.getAreaName());
		values.put(SearchRecord.R_PNAME, t.getPositionName());
		values.put(SearchRecord.R_TNAME, t.getTradeName());
		values.put(SearchRecord.R_ACODE, t.getAreaCode());
		values.put(SearchRecord.R_PCODE, t.getPositionCode());
		values.put(SearchRecord.R_TCODE, t.getTradeCode());
		return helper.insertData(CreateDbConstants.TABLE_RECORD, values);
	}

	@Override
	public List<SearchRecord> queryAll() {
		// TODO Auto-generated method stub
		List<SearchRecord> list = new ArrayList<SearchRecord>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CreateDbConstants.TABLE_RECORD, null);
		while (cursor.moveToNext()) {
			SearchRecord obj = new SearchRecord();
			obj.setAreaCode(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_ACODE)));
			obj.setAreaName(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_ANAME)));
			obj.setKeyWord(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_KEY)));
			obj.setRecordCount(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_COUNT)));
			obj.setPositionCode(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_PCODE)));
			obj.setPositionName(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_PNAME)));
			obj.setTradeCode(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_TCODE)));
			obj.setTradeName(cursor.getString(cursor
					.getColumnIndex(SearchRecord.R_TNAME)));
			
			list.add(obj);
		}
		return list;
	}
	public void deleteTable(){
		helper.deleteTable(CreateDbConstants.TABLE_RECORD);
	}
}
